<?php

/**
 * 历史数据的处理
*/
apf_require_class('Oa_Job_Attendance_Abstract');
apf_require_class("Tools_Tools");

apf_require_class('Const_LeaveType');
apf_require_class('Bll_Attendance_DailyReport');
apf_require_class('Bll_Attendance_DailyLeave');
apf_require_class('Bll_Attendance_AccessDayRecord');

class Oa_Job_Attendance_TmpPmStatus extends Oa_Job_Attendance_Abstract {
    private $readdb = "attendance_slave";
    /** 考勤系统写 **/
    private $writedb = "attendance_master";

    public function run($params){
        $this->date = $params['date'];
        if(empty($this->date)){
            $this->date = date('Ymd');
        }
        $all_init_records = $this->_get_all_user_day_record();
        $sales_and_not_shanghai = $this->_get_sale_and_not_in_shanghai();
        $tmp_record_list = $this->_get_all_am_records();
        $in_shanghai = $this->_in_shanghai();
        $record_list=array();
        foreach($tmp_record_list as $val){
            $record_list[$val['user_id']]=$val;
        }
        $bll = new Bll_Attendance_DailyReport();
        foreach($all_init_records as $init_record){
            //销售 非上海
            if(in_array($init_record['user_id'], $sales_and_not_shanghai)){
                continue;
            }
            //如果有请假或公出或出差的批准
            if($init_record['pm_leave_status'] == Const_LeaveType::LEAVE_STATUS_PASS){
                continue;
            }
            $status=$bll->get_one_accessday($init_record['user_id'],$this->date);
            if($this->date==20130201 && !empty($in_shanghai[$init_record['user_id']])){
                $cks = $this->_sp_day($init_record['user_id']);
                if($cks){
                    $status['status'][1]=6;
                }
            }
            if($status['status'][0]==5 || $status['status'][1]==6){
                $_where=array();
                if($status['status'][0]==5){
                    $_where['am_status']=5;
                }
                if($status['status'][1]==6){
                    $_where['pm_status']=6;
                }
                $this->change_user_status($_where,$init_record['user_id']);
            }
        }
    }
    private function _in_shanghai(){
        $sql = "SELECT user_id,department_name,department_id,city_name,enter_date FROM user_info WHERE city_name='上海' AND job_status='在职' ORDER BY user_id asc";
        $tmp = $this->get_db_rows_assoc($sql,$this->writedb,array());
        return $tmp;
    }
    private function _sp_day($user_id){
        $this->sql = "SELECT * FROM access_day_record WHERE access_date = ".$this->date." AND user_id=".$user_id;
        $data=$this->get_db_row($this->sql,$this->writedb,array());
        if(empty($data['user_id'])){
             return false;
        }
        $s_time = str_replace(':','',$data['start_work']);
        $e_time = str_replace(':','',$data['end_work']);
        if($s_time >1800){
              return false;
        }
        return true;
    }

    /**
     * 调整处理最终状态
     * @param array $data
     * **/
    private function change_user_status($data,$uid) {
        if (empty($data) || empty($uid)) {
            return;
        }
        $value = '';
        foreach ($data as $key => $val) {
            $value .= $key." = '{$val}',";
        }
        $value = substr($value,0,-1);
        $sql = "UPDATE user_day_status SET $value WHERE user_id = {$uid} AND dateline = {$this->date}";
        $this->execute($sql,array(),$this->writedb);
    }
    /**
     * 获取当日9：40之前的打卡记录
     */
    private function _get_all_am_records(){
        $this->sql = "SELECT * FROM access_day_record WHERE access_date = ".$this->date;
        return $this->get_db_rows($this->sql,$this->writedb,array());
    }
    /**
     * 获取所有用户初始打卡记录
     * @return multitype:
     */
    private function _get_all_user_day_record(){
        $this->sql = "SELECT * FROM user_day_status WHERE dateline={$this->date}";
        return $this->get_db_rows($this->sql,$this->writedb,array());
    }
    /**
     * 获取所有销售人员和非上海人员
     */
    private function _get_sale_and_not_in_shanghai(){
        $result = array();
        $last_timetamp=strtotime(date('Y-m-d'));
        $sale_department = APF::get_instance()->get_config('sale_department');
        $city_card = APF::get_instance()->get_config('city_card');
        $city_names = array_values($city_card);

        $start = 0;
        $limit = 100;
        while(true) {
            $sql = "SELECT user_id,department_name,department_id,city_name,enter_date FROM user_info WHERE user_id > {$start} AND job_status='在职' ORDER BY user_id asc limit {$limit}";
            $tmp = $this->get_db_rows($sql,$this->writedb,array());
            if(empty($tmp)) {
                $this->log("取完所有用户数据!");
                break;
            }
            $count = count($tmp);
            $start = $tmp[$count - 1]['user_id'];
            $result = array_merge($result,$tmp);
        }
        //过滤未入职员工
        $res = array();
        foreach ($result as $index => $value){
            if(in_array($value['city_name'],$city_names)){//非打卡城市
                continue;
            }
            $enter_time = strtotime($value['enter_date']);
            if($enter_time > $last_timetamp){
                continue;
            }
            if(empty($sale_department[$value['department_id']])){
                continue;
            }
            $res[$value['user_id']] = $value['user_id'];
        }
        return $res;
    }
}
